package tsg.graphic;

import java.util.Stack;

import tsg.util.Point;

public class Color implements Paint
{
	public static final Color RED   = new Color (255,   0,   0);
	public static final Color GREEN = new Color (  0, 255,   0);
	public static final Color BLUE  = new Color (  0,   0, 255);
	public static final Color WHITE = new Color (255, 255, 255);
	public static final Color BLACK = new Color (  0,   0,   0);
	public static final Color GREY  = new Color (125, 125, 125);
	
	public Color()
	{
		
	}
	
	public Color(Color color)
	{
		this(color.getRed(), color.getGreen(), color.getBlue());
	}
	
	public Color(int red, int green, int blue) 
	{
		this.setRed(red);
		this.setGreen(green);
		this.setBlue(blue);
	}

	public int getRed()
	{
		return _red;
	}
	
	public void setRed(int red)
	{
		_red = red;
	}
	
	
	public int getGreen()
	{
		return _green;
	}
	
	public void setGreen(int green)
	{
		_green = green;
	}
	
	
	public int getBlue()
	{
		return _blue;
	}
	
	public void setBlue(int blue)
	{
		_blue = blue;
	}
	
	public void paint(Graphics g, Point origin) 
	{
		g.drawLine(origin.getX(), origin.getY(), origin.getX(), origin.getY());
	}
	
	public void fill(Graphics g, Point origin, boolean[][] boundingMap) 
	{
		boolean[][]  fillMap      = new boolean[boundingMap.length][boundingMap[0].length];
		int          width        = fillMap[0].length;
		int          height       = fillMap.length;
		Stack<Point> needToFill   = new Stack<Point>();
		Point        currentPoint = origin;
		
		g.setColor(this);
		
		needToFill.add(currentPoint);
		
		
		while(needToFill.size() > 0)
		{
			// Get the current working point
			currentPoint = needToFill.pop();
			
			// Paint this point
			g.drawLine(currentPoint.getX(), currentPoint.getY(), currentPoint.getX(), currentPoint.getY());
			
			// Mark this point as filled
			fillMap[currentPoint.getY()][currentPoint.getX()] = true;
			
			// Do we need to fill upward?
			if( currentPoint.getY() 									>  0				&&  // Make sure we are not at the top
				fillMap[currentPoint.getY()-1][currentPoint.getX()]		== false			&&	// Make sure the pixel above has not yet been filled
				boundingMap[currentPoint.getY()-1][currentPoint.getX()]	== false			)	// Make sure the pixel above is not bounded
			{
				needToFill.add(new Point(currentPoint.getX(), currentPoint.getY()-1));
			}


			// Do we need to fill downward?
			if( currentPoint.getY() 									<  height			&&  // Make sure we are not at the bottom
				fillMap[currentPoint.getY()+1][currentPoint.getX()]		== false			&&	// Make sure the pixel below has not yet been filled
				boundingMap[currentPoint.getY()+1][currentPoint.getX()]	== false			)	// Make sure the pixel below is not bounded
			{
				needToFill.add(new Point(currentPoint.getX(), currentPoint.getY()+1));
			}
			
			
			// Do we need to fill left?
			if( currentPoint.getX() 									>  0				&&  // Make sure we are not at the left
				fillMap[currentPoint.getY()][currentPoint.getX()-1]		== false			&&	// Make sure the pixel left has not yet been filled
				boundingMap[currentPoint.getY()][currentPoint.getX()-1]	== false			)	// Make sure the pixel left is not bounded
			{
				needToFill.add(new Point(currentPoint.getX()-1, currentPoint.getY()));
			}
			
			
			// Do we need to fill right?
			if( currentPoint.getY() 									<  width			&&  // Make sure we are not at the right
				fillMap[currentPoint.getY()][currentPoint.getX()+1]		== false			&&	// Make sure the pixel right has not yet been filled
				boundingMap[currentPoint.getY()][currentPoint.getX()+1]	== false			)	// Make sure the pixel right is not bounded
			{
				needToFill.add(new Point(currentPoint.getX()+1, currentPoint.getY()));
			}	
		}
	}
	
	
	
	private int _red	= 0;
	private int _green	= 0;
	private int _blue	= 0;
}
